clear all;
%parpool close force local;
parpool(4);
%Setting Options

optionsLoose = optimset('TolX', 1e-5, 'TolFun',1e-5, 'MaxFunEvals', 1e5, 'MaxIter', 1e5);
optionsTight = optimset('TolX', 1e-8, 'TolFun',1e-8, 'MaxFunEvals', 1e8, 'MaxIter', 1e8);

UtilFuncParam = 1;
ChoiceProbForm = 1;
NumBootReps = 250;
RandomizationSeed = 12345;
%%
%Loading Data
DATA = csvread('CCES_Matlab_Round.csv', 1, 0);


VoteDem = DATA(:,1);
VoterIdeal = DATA(:,2);
RepPartyIdeal = DATA(:,3);
DemPartyIdeal = DATA(:,4);
RepCandIdeal = DATA(:,5);
DemCandIdeal = DATA(:,6);

VoterIdeal_Round = (round(6*(VoterIdeal)))/6;
RepPartyIdeal_Round = (round(6*RepPartyIdeal))/6; 
DemPartyIdeal_Round = (round(6*DemPartyIdeal))/6; 
RepCandIdeal_Round = (round(6*RepCandIdeal))/6; 
DemCandIdeal_Round = (round(6*DemCandIdeal))/6; 


IncRep = DATA(:,7);
IncDem = DATA(:,8);
ExpChallRep = DATA(:,9);
ExpChallDem = DATA(:,10);
InExpChallRep = DATA(:,11);
InExpChallDem = DATA(:,12);
Year2008 = DATA(:,13);
Year2010 = DATA(:,14);
Year2012 = DATA(:,15);
Age = DATA(:,16);
State_FE=DATA(:,17:65);
Dist_Index = DATA(:,67); 
Dist_FE = DATA(:,68:501);

Dem = (DATA(:,505) == 1);
Rep = (DATA(:,505) == 2);

CandTypeDem = horzcat(IncDem, ExpChallDem, InExpChallDem);
CandTypeRep = horzcat(IncRep, ExpChallRep, InExpChallRep);
clear DATA;


Lambdas = vertcat(0.5, 0.5, 0.5);

%%
RHS = [Year2008, Year2010, Year2012];
Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);

[thetaTightProbit1G, fvalProbit1G] = fminunc(@(Theta) IndivVoterObjFun(Theta,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);

[SEsBoot1 thetaBoot1 exitflagBoot1] = getVarCovBootstrapCluster(thetaTightProbit1G, VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, Dist_Index);
[IncAdv_Mat_Probit1] = getIncAdv(thetaTightProbit1G,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, 1,Dist_Index);

for b = 1:NumBootReps
    [IncAdv_Mat_Probit1_Boot(:,b)] = getIncAdv(thetaBoot1(:,b) ,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, 1,Dist_Index);
end

IncAdv_Mat_Probit1_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit1_Boot - repmat(mean(IncAdv_Mat_Probit1_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('thetaTightProbit1_EU_Round.csv', thetaTightProbit1G);
csvwrite('fvalProbit1_EU_Round.csv', fvalProbit1G);
csvwrite('IncAdv_Mat_Probit1_EU_Round.csv', IncAdv_Mat_Probit1);
csvwrite('thetaBoot_Probit1_EU_Round.csv', thetaBoot1);
csvwrite('IncAdv_Mat_Probit1_Boot_EU_Round.csv', IncAdv_Mat_Probit1_Boot);
csvwrite('IncAdv_Mat_Probit1_SE_EU_Round.csv', IncAdv_Mat_Probit1_SE);


RHS = [Year2008, Year2010, Year2012, State_FE];
Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);
[thetaTightProbit2G, fvalProbit2G] = fminunc(@(Theta) IndivVoterObjFun(Theta,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);

[SEsBoot2 thetaBoot2 exitflagBoot2] = getVarCovBootstrapCluster(thetaTightProbit2G, VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, Dist_Index);

[IncAdv_Mat_Probit2] = getIncAdv(thetaTightProbit2G,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, 1,Dist_Index);

for b = 1:NumBootReps
    [IncAdv_Mat_Probit2_Boot(:,b)] = getIncAdv(thetaBoot2(:,b) ,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, 1,Dist_Index);
end

IncAdv_Mat_Probit2_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit2_Boot - repmat(mean(IncAdv_Mat_Probit2_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('thetaTightProbit2_EU_Round.csv', thetaTightProbit2G);
csvwrite('fvalProbit2_EU_Round.csv', fvalProbit2G);
csvwrite('IncAdv_Mat_Probit2_EU_Round.csv', IncAdv_Mat_Probit2);
csvwrite('thetaBoot_Probit2_EU_Round.csv', thetaBoot2);
csvwrite('IncAdv_Mat_Probit2_Boot_EU_Round.csv', IncAdv_Mat_Probit2_Boot);
csvwrite('IncAdv_Mat_Probit2_SE_EU_Round.csv', IncAdv_Mat_Probit2_SE);

%%
RHS = [Year2008, Year2010, Year2012, Dist_FE];

Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);
[thetaTightProbit3G, fvalProbit3G] = fminunc(@(Theta) IndivVoterObjFun(Theta,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);

[SEsBoot3 thetaBoot3 exitflagBoot3] = getVarCovBootstrapCluster(thetaTightProbit3G, VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, Dist_Index);

[IncAdv_Mat_Probit3] = getIncAdv(thetaTightProbit3G,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, 1,Dist_Index);

for b = 1:NumBootReps
    [IncAdv_Mat_Probit3_Boot(:,b)] = getIncAdv(thetaBoot3(:,b) ,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, 1,Dist_Index);
end

IncAdv_Mat_Probit3_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit3_Boot - repmat(mean(IncAdv_Mat_Probit3_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('thetaTightProbit3_EU_Round.csv', thetaTightProbit3G);
csvwrite('fvalProbit3_EU_Round.csv', fvalProbit3G);
csvwrite('IncAdv_Mat_Probit3_EU_Round.csv', IncAdv_Mat_Probit3);
csvwrite('thetaBoot_Probit3_EU_Round.csv', thetaBoot3);
csvwrite('IncAdv_Mat_Probit3_Boot_EU_Round.csv', IncAdv_Mat_Probit3_Boot);
csvwrite('IncAdv_Mat_Probit3_SE_EU_Round.csv', IncAdv_Mat_Probit3_SE);



RandomizationSeed = 12345;

DATA = csvread('CCES_Matlab_IndivCovariates_Round.csv', 1, 0);

Dem = (DATA(:,515) == 1);
Rep = (DATA(:,515) == 2);


VoteDem = DATA(:,1);
VoterIdeal = DATA(:,2);
RepPartyIdeal = DATA(:,3);
DemPartyIdeal = DATA(:,4);
RepCandIdeal = DATA(:,5);
DemCandIdeal = DATA(:,6);

VoterIdeal_Round = (round(6*(VoterIdeal)))/6;
RepPartyIdeal_Round = (round(6*RepPartyIdeal))/6; 
DemPartyIdeal_Round = (round(6*DemPartyIdeal))/6; 
RepCandIdeal_Round = (round(6*RepCandIdeal))/6; 
DemCandIdeal_Round = (round(6*DemCandIdeal))/6; 


IncRep = DATA(:,7);
IncDem = DATA(:,8);
ExpChallRep = DATA(:,9);
ExpChallDem = DATA(:,10);
InExpChallRep = DATA(:,11);
InExpChallDem = DATA(:,12);
Year2008 = DATA(:,13);
Year2010 = DATA(:,14);
Year2012 = DATA(:,15);
Age = DATA(:,16);
State_FE=DATA(:,17:65);
Dist_Index = DATA(:,67); 
Dist_FE = DATA(:,68:501);

HighSchool = DATA(:,503);
TwoYearCollege = DATA(:,504);
FourYearCollege = DATA(:,505);
PostBA = DATA(:,506);
Female = DATA(:,507);
Af_Am = DATA(:,508);
Latino = DATA(:,509);
Asian = DATA(:,510);
AttendRelServiceWeek = DATA(:,511);
Income = DATA(:,512);


CandTypeDem = horzcat(IncDem, ExpChallDem, InExpChallDem);
CandTypeRep = horzcat(IncRep, ExpChallRep, InExpChallRep);
cd('~/Box Sync/IdeolSig copy/CCES/')



RHS = [Year2008, Year2010, Year2012, Dist_FE, HighSchool, TwoYearCollege, FourYearCollege, PostBA, Female, Af_Am, Latino, Asian, AttendRelServiceWeek, Income, Age];

Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);
[thetaTightProbit4G, fvalProbit4G] = fminunc(@(Theta) IndivVoterObjFun(Theta,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);

[IncAdv_Mat_Probit4, IncAdvExpChallDem_Weighted_Holder4, IncAdvExpChallRep_Weighted_Holder4, IncAdvInExpChallDem_Weighted_Holder4, IncAdvInExpChallRep_Weighted_Holder4] = getIncAdv(thetaTightProbit4G,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, 1,Dist_Index);

cd('~/Box Sync/IdeolSig copy/CCES/BJPS_RR')

csvwrite('thetaTightProbit4_EU_Round.csv', thetaTightProbit4G);
csvwrite('fvalProbit4_EU_Round.csv', fvalProbit4G);
csvwrite('IncAdv_Mat_Probit4_EU_Round.csv', IncAdv_Mat_Probit4);
cd('~/Box Sync/IdeolSig copy/CCES/')

%parpool(4);
[SEsBoot4 thetaBoot4 exitflagBoot4] = getVarCovBootstrapCluster(thetaTightProbit4G, VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, Dist_Index);


for b = 1:NumBootReps
    [IncAdv_Mat_Probit4_Boot(:,b)] = getIncAdv(thetaBoot4(:,b) ,VoteDem, VoterIdeal_Round, CandTypeDem, CandTypeRep, DemCandIdeal_Round, DemPartyIdeal_Round, RepCandIdeal_Round, RepPartyIdeal_Round, RHS, UtilFuncParam, 1,Dist_Index);
end

IncAdv_Mat_Probit4_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit4_Boot - repmat(mean(IncAdv_Mat_Probit4_Boot,2), 1, NumBootReps)).^2,2));

cd('~/Box Sync/IdeolSig copy/CCES/BJPS_RR')
csvwrite('thetaBoot_Probit4_EU_Round.csv', thetaBoot4);
csvwrite('IncAdv_Mat_Probit4_Boot_EU_Round.csv', IncAdv_Mat_Probit4_Boot);
csvwrite('IncAdv_Mat_Probit4_SE_EU_Round.csv', IncAdv_Mat_Probit4_SE);